package com.example.myapplication.provider;

import com.example.myapplication.ResourceTable;
import com.example.myapplication.entity.DataItem;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.sysappcomponents.contact.entity.Contact;


import java.util.List;

public class DataItemProvider extends BaseItemProvider {
    private static final int DIALOG_WIDTH = 900;

    private static final int DIALOG_HEIGHT = 600;

    private static final int DIALOG_CORNER_RADIUS = 30;

    private CommonDialog commonDialog;

    private List<DataItem> dataItemData;

    private final Context context;

    public DataItemProvider(Context context,List<DataItem> dataItemData) {
        this.context = context;
        this.dataItemData = dataItemData;
    }


    @Override
    public int getCount() {
        return dataItemData.size();
    }

    @Override
    public Object getItem(int i) {
        return dataItemData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        return getItemComponent(i);
    }



    private Component getItemComponent(int position) {
        return getComponent(position);
    }

    private Component getComponent(int position) {
        DataItem dataItem = dataItemData.get(position);
        Component container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_list_item, null, false);
        Text name = (Text) container.findComponentById(ResourceTable.Id_name);
        Text tel = (Text) container.findComponentById(ResourceTable.Id_text);
        Component btnEdit = container.findComponentById(ResourceTable.Id_btn_edit);
        Component btnDelete = container.findComponentById(ResourceTable.Id_btn_delete);
        name.setText(dataItem.getName());
        tel.setText(dataItem.getText());
        btnEdit.setClickedListener(component -> showContactDetailDialog(dataItem, position));
        btnDelete.setClickedListener(component -> deleteDialog(position));
        return container;
    }

    private void deleteDialog(int position) {
        Component container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_delete_succeed, null, false);
        Button btnOk = (Button) container.findComponentById(ResourceTable.Id_btn_ok);
        Button btnCancel = (Button) container.findComponentById(ResourceTable.Id_btn_cancel);

        btnOk.setClickedListener(component -> {
            dataItemData.remove(position);
            notifyDataChanged();
            commonDialog.destroy();
        });
        btnCancel.setClickedListener(component -> {
            notifyDataChanged();
            commonDialog.destroy();
        });
        commonDialog = new CommonDialog(context);
        commonDialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
        commonDialog.setCornerRadius(DIALOG_CORNER_RADIUS);
        commonDialog.setContentCustomComponent(container);
        commonDialog.show();
    }

    private void showContactDetailDialog(DataItem contact, int position) {
        Component container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_layout, null, false);
        TextField name = (TextField) container.findComponentById(ResourceTable.Id_name);
        name.setText(contact.getName());
        TextField text = (TextField) container.findComponentById(ResourceTable.Id_text);
        text.setText(contact.getText());
        Button btnOk = (Button) container.findComponentById(ResourceTable.Id_btn_ok);

        btnOk.setClickedListener(component -> saveEdit(name.getText(), text.getText(), position));
        commonDialog = new CommonDialog(context);
        commonDialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
        commonDialog.setCornerRadius(DIALOG_CORNER_RADIUS);
        commonDialog.setContentCustomComponent(container);
        commonDialog.show();
    }

    private void saveEdit(String name, String text, int position) {
        dataItemData.get(position).setName(name);
        dataItemData.get(position).setText(text);
        notifyDataChanged();
        commonDialog.destroy();
    }

    /**
     * update list data
     *
     * @param data contactDevicesData list
     */
    public void update(List<DataItem> data) {
        dataItemData = data;
        notifyDataChanged();
    }



}
